﻿using BW.EatRabbit.API.Application.Commands.Picture;
using BW.EatRabbit.Domain;
using BW.EatRabbit.ErrorCode;
using BW.EatRabbit.Infrastructure.Interfaces;
using MediatR;
using System.Diagnostics;

namespace BW.EatRabbit.API.Application.CommandHandlers.Picture
{
    public class PictureCreateCommandHandler : IRequestHandler<PictureCreateCommand, APIResult<int>>
    {
        private readonly IBaseRepository<Picture_Information> _pictureRepository;
        private readonly IBaseRepository<BasicInformation> _basicinformationRepository;
        private readonly IBaseRepository<Breeding_Information> _breedingRepository;
        private readonly IBaseRepository<Farming_Company> _farmingRepository;
        private readonly IBaseRepository<Slaughter_Information> _slauhterRepository;
        private readonly IBaseRepository<DeepProcessing_Company> _deepProcessRepository;
        private readonly IBaseRepository<Provider_Information> _providerRepository;
        public PictureCreateCommandHandler(IBaseRepository<Picture_Information> pictureRepository, IBaseRepository<BasicInformation> basicinformationRepository,
            IBaseRepository<Breeding_Information> breedingRepository, IBaseRepository<Farming_Company> farmingRepository,
            IBaseRepository<Slaughter_Information> slauhterRepository, IBaseRepository<DeepProcessing_Company> deepProcessRepository, 
            IBaseRepository<Provider_Information> providerRepository)
        {
            _pictureRepository = pictureRepository;
            _basicinformationRepository = basicinformationRepository;
            _breedingRepository =breedingRepository;
            _farmingRepository = farmingRepository;
            _slauhterRepository = slauhterRepository;
            _deepProcessRepository = deepProcessRepository;
            _providerRepository = providerRepository;

        }
        public Task<APIResult<int>> Handle(PictureCreateCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();

      

            var code=_basicinformationRepository.GetAll().FirstOrDefault(x=>x.ProductionBatch==request.ProductionBatch);
            Picture_Information picture = new Picture_Information();
            if (code==null)
            {
                result.Code = ResultCode.添加失败;
                result.Message = "上传失败";

            }
            else
            {
               
                picture.ProducReport=request.ProducReport;

                _pictureRepository.AddEntity(picture);
                picture.Creator = request.UserNickname;
                result.Code = ResultCode.添加成功;
                result.Message = "上传成功";
                return Task.FromResult(result);
            }
            var ment=_breedingRepository.GetAll().FirstOrDefault(x=>x.ProductionBatch!=request.ProductionBatch);
            if (ment == null)
            {
                result.Code = ResultCode.添加失败;
                result.Message = "上传失败";
            }
            else
            {
              
                picture.Livestock = request.Livestock;
                picture.Creator = request.UserNickname;
                _pictureRepository.AddEntity(picture);

                result.Code = ResultCode.添加成功;
                result.Message = "上传成功";
                return Task.FromResult(result);
            }

            Farming_Company? side = _farmingRepository.GetAll().FirstOrDefault(x => x.ProductionBatch == request.ProductionBatch);
            if (side == null)
            {
                result.Code = ResultCode.添加失败;
                result.Message = "上传失败";
            }
            else
            {
              
                picture.CompanyPhoto = request.CompanyPhoto;

                _pictureRepository.AddEntity(picture);
                picture.Creator = request.UserNickname;
                result.Code = ResultCode.添加成功;
                result.Message = "上传成功";
                return Task.FromResult(result);
            }

            var slaugter = _slauhterRepository.GetAll().FirstOrDefault(x => x.ProductionBatch == request.ProductionBatch);

            if (slaugter == null)
            {
                result.Code = ResultCode.添加失败;
                result.Message = "上传失败";
            }
            else
            {
              
                picture.Certification_Documents = request.Certification_Documents;

                _pictureRepository.AddEntity(picture);
                picture.Creator = request.UserNickname;
                result.Code = ResultCode.添加成功;
                result.Message = "上传成功";
                return Task.FromResult(result);
            }

            var deep = _deepProcessRepository.GetAll().FirstOrDefault(x=>x.ProductionBatch==request.ProductionBatch);
            if (deep == null)
            {
                result.Code = ResultCode.添加失败;
                result.Message = "上传失败";
            }
            else
            {
           
                picture.Food_Safety = request.Food_Safety;

                _pictureRepository.AddEntity(picture);
                picture.Creator = request.UserNickname;
                result.Code = ResultCode.添加成功;
                result.Message = "上传成功";
                return Task.FromResult(result);
            }
            var node= _providerRepository.GetAll().FirstOrDefault(x=>x.ProductionBatch==request.ProductionBatch);
            if (node.ProviderType == 1)
            {
                
                picture.Materials = request.Materials;

                _pictureRepository.AddEntity(picture);
                picture.Creator = request.UserNickname;
                result.Code = ResultCode.添加成功;
                result.Message = "上传成功";
                return Task.FromResult(result);
            }
            else if (node.ProviderType == 2)
            {
              
                picture.Accessories = request.Accessories;

                _pictureRepository.AddEntity(picture);
                picture.Creator = request.UserNickname;
                result.Code = ResultCode.添加成功;
                result.Message = "上传成功";
                return Task.FromResult(result);
            }
            else if(node.ProviderType==3)
            {
              
                picture.Additive = request.Additive;
                picture.Creator = request.UserNickname;
                _pictureRepository.AddEntity(picture);

                result.Code = ResultCode.添加成功;
                result.Message = "上传成功";
                return Task.FromResult(result);
            }
            else if(node.ProviderType == 4)
            {
               
                picture.Packaging = request.Packaging;
                picture.Creator = request.UserNickname;
                _pictureRepository.AddEntity(picture);

                result.Code = ResultCode.添加成功;
                result.Message = "上传成功";
                return Task.FromResult(result);
            }

            

            return Task.FromResult(result);


        }
    }
}
